﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
   public static class GoiHang
    {
        public static Entities.GoiHang[] Select(Entities.GoiHang nv)
        {
            Entities.GoiHang[] list = null;
            try
            {
                string sql = Common.Constants.GoiHang.SelectGoiHang;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.GoiHang.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.GoiHang row = new Entities.GoiHang();
                    row.GoiHangID = int.Parse(dr[Common.Constants.GoiHang.GoiHangID].ToString());
                    row.MaGoiHang = dr[Common.Constants.GoiHang.MaGoiHang].ToString();
                    row.TenGoiHang = dr[Common.Constants.GoiHang.TenGoiHang].ToString();
                    row.MaNhomHang = dr[Common.Constants.GoiHang.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.GoiHang.TenNhomHang].ToString();
                    row.GiaNhap = dr[Common.Constants.GoiHang.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.GoiHang.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.GoiHang.GiaBanLe].ToString();
                    row.HanSuDung = DateTime.Parse(dr[Common.Constants.GoiHang.HanSuDung].ToString());
                    row.GhiChu = dr[Common.Constants.GoiHang.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.GoiHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.GoiHang[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.GoiHang)arr[i];
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            return list;
        }
        public static Entities.GoiHang[] SelectTheoMa(Entities.GoiHang hh)
        {
            Entities.GoiHang[] list = null;
            try
            {
                string sql = Common.Constants.GoiHang.SelectTheoMaGoiHang;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.GoiHang.MaGoiHang, SqlDbType.VarChar, 50).Value = hh.MaGoiHang;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.GoiHang row = new Entities.GoiHang();
                    row.GoiHangID = int.Parse(dr[Common.Constants.GoiHang.GoiHangID].ToString());
                    row.MaGoiHang = dr[Common.Constants.GoiHang.MaGoiHang].ToString();
                    row.TenGoiHang = dr[Common.Constants.GoiHang.TenGoiHang].ToString();
                    row.MaNhomHang = dr[Common.Constants.GoiHang.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.GoiHang.TenNhomHang].ToString();
                    row.GiaBanBuon = dr[Common.Constants.GoiHang.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.GoiHang.GiaBanLe].ToString();
                    row.HanSuDung = DateTime.Parse(dr[Common.Constants.GoiHang.HanSuDung].ToString());
                    row.GhiChu = dr[Common.Constants.GoiHang.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.GoiHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.GoiHang[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.GoiHang)arr[i];
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            return list;
        }
        public static int InsertUpdate(Entities.GoiHang nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.GoiHang.InsertUpdateGoiHang, cn);
                // @HanhDong,@GoiHangID,@MaGoiHang,@TenGoiHang,@MaNhomHang,@TenNhomHang,@GiaNhap,@GiaBanBuon,@GiaBanLe,@HanSuDung,@GhiChu,@Deleted
                cmd.Parameters.Add(Common.Constants.GoiHang.HanhDong, SqlDbType.NVarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.GoiHang.GoiHangID, SqlDbType.Int).Value = nhom.GoiHangID;
                cmd.Parameters.Add(Common.Constants.GoiHang.MaGoiHang, SqlDbType.VarChar, 50).Value = nhom.MaGoiHang.ToUpper();
                cmd.Parameters.Add(Common.Constants.GoiHang.TenGoiHang, SqlDbType.NVarChar, 200).Value = nhom.TenGoiHang;
                cmd.Parameters.Add(Common.Constants.GoiHang.MaNhomHang, SqlDbType.VarChar, 50).Value = nhom.MaNhomHang;
                cmd.Parameters.Add(Common.Constants.GoiHang.TenNhomHang, SqlDbType.NVarChar, 200).Value = nhom.TenNhomHang;
                cmd.Parameters.Add(Common.Constants.GoiHang.GiaNhap, SqlDbType.Float).Value = nhom.GiaNhap;
                cmd.Parameters.Add(Common.Constants.GoiHang.GiaBanBuon, SqlDbType.Float).Value = nhom.GiaBanBuon;
                cmd.Parameters.Add(Common.Constants.GoiHang.GiaBanLe, SqlDbType.Float).Value = nhom.GiaBanLe;
                cmd.Parameters.Add(Common.Constants.GoiHang.HanSuDung, SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add(Common.Constants.GoiHang.GhiChu, SqlDbType.NVarChar,200).Value = nhom.GhiChu;
                cmd.Parameters.Add(Common.Constants.GoiHang.Deleted, SqlDbType.Bit).Value = nhom.Deleted;
                rt = cmd.ExecuteNonQuery();
                
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }

        public static int Delete(Entities.GoiHang nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.GoiHang.DeleteGoiHang, cn);
                cmd.Parameters.Add(Common.Constants.GoiHang.HanhDong, SqlDbType.VarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.GoiHang.MaGoiHang, SqlDbType.VarChar).Value = nhom.MaGoiHang;
                rt = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }


        /// <summary>
        /// loivt
        /// </summary>
        /// <returns></returns>
        public static Entities.GoiHang[] Select()
        {
            Entities.GoiHang[] list = null;
            try
            {
                string sql = Common.Constants.GoiHang.SelectGoiHang;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.GoiHang row = new Entities.GoiHang();
                    row.GoiHangID = int.Parse(dr[Common.Constants.GoiHang.GoiHangID].ToString());
                    row.MaGoiHang = dr[Common.Constants.GoiHang.MaGoiHang].ToString();
                    row.TenGoiHang = dr[Common.Constants.GoiHang.TenGoiHang].ToString();
                    row.MaNhomHang = dr[Common.Constants.GoiHang.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.GoiHang.TenNhomHang].ToString();
                    row.GiaNhap = dr[Common.Constants.GoiHang.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.GoiHang.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.GoiHang.GiaBanLe].ToString();
                    row.HanSuDung = DateTime.Parse(dr[Common.Constants.GoiHang.HanSuDung].ToString());
                    row.GhiChu = dr[Common.Constants.GoiHang.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.GoiHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.GoiHang[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.GoiHang)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            return list;
        }
    }
}
